package com.app.micaihu.ad.toutiao;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.app.micaihu.R;
import com.blankj.utilcode.util.LogUtils;
import com.bytedance.sdk.openadsdk.FilterWord;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.List;

/**
 * 头条广告样式一(列表小图样式)
 *
 * @author linkai
 */
public class ToutiaoNativeView extends RelativeLayout {


    private int adPosition;
    private String toutiaoDelKey;
    private TTNativeExpressAd adView;

    public ToutiaoNativeView(Context context) {
        super(context);
        init(context);
    }

    public void setAdPosition(int position) {
        this.adPosition = position;
    }

    public void setAdDelKey(String toutiaoDelKey) {
        this.toutiaoDelKey = toutiaoDelKey;
        LogUtils.e("toutiao setAdDelKey->" + toutiaoDelKey);
    }

    public ToutiaoNativeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ToutiaoNativeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        initView(context);
    }

    /**
     * 初始化广告view
     *
     * @param context
     */
    private void initView(Context context) {
        inflate(context, R.layout.item_toutiao_express_ad, this);
    }

    public void setAdView(TTNativeExpressAd adView) {
        if (adView == null) return;
        this.adView = adView;
        View expressAdView = adView.getExpressAdView();
        if (expressAdView == null) return;
        if (getChildCount() > 0 && getChildAt(0) == adView) {
            return;
        }
        if (getChildCount() > 0) {
            removeAllViews();
        }
        if (expressAdView.getParent() != null) {
            ((ViewGroup) expressAdView.getParent()).removeView(expressAdView);
        }
        addView(expressAdView);
//        adView.render();
        adView.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int i) {

            }

            @Override
            public void onAdShow(View view, int i) {
//                LogUtils.e("toutiao onAdShow", i);
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
//                LogUtils.e("toutiao onRenderFail", i, s);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
            }
        });
        delDialogView(adView);
    }

    private void delDialogView(TTNativeExpressAd adView) {
        //使用自定义样式
        List<FilterWord> words = adView.getFilterWords();
        if (words == null || words.isEmpty()) {
            return;
        }
        ToutiaoCloseDialog toutiaoCloseDialog = new ToutiaoCloseDialog(getContext(), words);
        toutiaoCloseDialog.setOnDislikeItemClick(new ToutiaoCloseDialog.OnDislikeItemClick() {
            @Override
            public void onItemClick(FilterWord filterWord) {
                LogUtils.e("toutiao filterWord", filterWord.getName(), "del position->" + adPosition, "del key->" + toutiaoDelKey);
                LiveEventBus.get(toutiaoDelKey, Integer.class).post(adPosition);
            }
        });
        toutiaoCloseDialog.setCancelable(true);
        toutiaoCloseDialog.setCanceledOnTouchOutside(true);
        adView.setDislikeDialog(toutiaoCloseDialog);
    }

    /**
     * 加载一个广告
     *
     * @param adPosId
     */
    public void loadOneAd(final String adPosId, String style) {
        //通过广告类型和样式 获取之前预加载的广告的id
        String ad = "";
        //用获取到的广告id去获取预加载的广告
        if (!TextUtils.isEmpty(ad)) {
            ToutiaoNativeAd bna = ToutiaoAdManager.getInstance().getAd(ad);
            if (bna != null) {
                TTNativeExpressAd nrAd = bna.getNativeAd();
                // 如果获取的广告不为null
                if (nrAd != null) {
                    // 添加到加载过的广告集合中
                    setAdView(nrAd);
                    return;
                }
            }
        }
        //如果最终还是没有获取到加载好的广告,则通过广告id去请求广告
        ToutiaoNativeAd bna = ToutiaoAdManager.getInstance().getAd(adPosId);
        if (bna != null) {
            bna.loadOneAd(this.getContext(), adPosId, new ToutiaoNativeAd.NativeAdLoadCallback() {

                @Override
                public void onNativeAdLoad(TTNativeExpressAd ad, boolean success) {
                    if (ad != null && success) {
                        setAdView(ad);
                    }
                }
            });
        }

    }

    @Override
    protected void onDetachedFromWindow() {
        LogUtils.e("newsAdWindow", "key");
        if (this.adView != null) {
            this.adView.destroy();
            this.adView = null;
        }
        super.onDetachedFromWindow();
    }

}
